package com.demo.controller;

import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo.entity.User;
import com.demo.service.IUserServices;

@Controller
@RequestMapping("/userinfo")
public class UserInfoController {
	@Autowired
	private IUserServices userService;

	/**
	 * @Description: 获取用户列表
	 * @author ZhangXiaoNan
	 * @date 2020-01-04 17:59:29
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/select", method = RequestMethod.GET)
	@ResponseBody
	public Object selectUserList() throws Exception {
		List<User> list = userService.selectUserList();
		return list;
	}

	int begin = 1;// 增加用户起始下标
	int end = 10;// 增加用户结束下标
	int add = 10;// 增加用户增量

	/**
	 * @Description: 增加用户
	 * @author ZhangXiaoNan
	 * @date 2020-01-04 17:59:48
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/insert")
	@ResponseBody
	public Object insert() throws Exception {

		for (int i = begin; i <= end; i++) {
			User userBase = new User();
			userBase.setId(i);
			userBase.setName("用户" + i);
			int randNumber = new Random().nextInt(45 - 18 + 1) + 18; // randNumber 将被赋值为一个 MIN 和 MAX 范围内
			userBase.setAge(randNumber);
			userService.save(userBase);
		}
		begin = begin + add;
		end = end + add;

		return "success";
	}

	/**
	 * @Description: 删除所有用户
	 * @author ZhangXiaoNan
	 * @date 2020-01-04 18:00:49
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Object delete() throws Exception {

		userService.delete();

		return "success";
	}
}
